<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Install OpenCV-Python in Ubuntu</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d6/d00/tutorial_py_root.html">OpenCV-Python Tutorials</a></li><li class="navelem"><a class="el" href="../../da/df6/tutorial_py_table_of_contents_setup.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Install OpenCV-Python in Ubuntu </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Goals </h2>
<p>In this tutorial We will learn to setup OpenCV-Python in Ubuntu System. Below steps are tested for Ubuntu 16.04 and 18.04 (both 64-bit).</p>
<p>OpenCV-Python can be installed in Ubuntu in two ways:</p><ul>
<li>Install from pre-built binaries available in Ubuntu repositories</li>
<li>Compile from the source. In this section, we will see both.</li>
</ul>
<p>Another important thing is the additional libraries required. OpenCV-Python requires only <b>Numpy</b> (in addition to other dependencies, which we will see later). But in this tutorials, we also use <b>Matplotlib</b> for some easy and nice plotting purposes (which I feel much better compared to OpenCV). Matplotlib is optional, but highly recommended. Similarly we will also see <b>IPython</b>, an Interactive Python Terminal, which is also highly recommended.</p>
<h2>Installing OpenCV-Python from Pre-built Binaries </h2>
<p>This method serves best when using just for programming and developing OpenCV applications.</p>
<p>Install package <a href="https://packages.ubuntu.com/focal/python3-opencv">python3-opencv</a> with following command in terminal (as root user).</p>
<div class="fragment"><div class="line">$ sudo apt-get install python3-opencv</div></div><!-- fragment --><p>Open Python IDLE (or IPython) and type following codes in Python terminal.</p>
<div class="fragment"><div class="line">import cv2 as cv</div><div class="line">print(cv.__version__)</div></div><!-- fragment --><p>If the results are printed out without any errors, congratulations !!! You have installed OpenCV-Python successfully.</p>
<p>It is quite easy. But there is a problem with this. Apt repositories may not contain the latest version of OpenCV always. For example, at the time of writing this tutorial, apt repository contains 2.4.8 while latest OpenCV version is 3.x. With respect to Python API, latest version will always contain much better support and latest bug fixes.</p>
<p>So for getting latest source codes preference is next method, i.e. compiling from source. Also at some point in time, if you want to contribute to OpenCV, you will need this.</p>
<h2>Building OpenCV from source </h2>
<p>Compiling from source may seem a little complicated at first, but once you succeeded in it, there is nothing complicated.</p>
<p>First we will install some dependencies. Some are required, some are optional. You can skip optional dependencies if you don't want.</p>
<h3>Required build dependencies</h3>
<p>We need <b>CMake</b> to configure the installation, <b>GCC</b> for compilation, <b>Python-devel</b> and <b>Numpy</b> for building Python bindings etc.</p>
<div class="fragment"><div class="line">sudo apt-get install cmake</div><div class="line">sudo apt-get install gcc g++</div></div><!-- fragment --><p> to support python2:</p>
<div class="fragment"><div class="line">sudo apt-get install python-dev python-numpy</div></div><!-- fragment --><p>to support python3:</p>
<div class="fragment"><div class="line">sudo apt-get install python3-dev python3-numpy</div></div><!-- fragment --><p>Next we need <b>GTK</b> support for GUI features, Camera support (v4l), Media Support (ffmpeg, gstreamer) etc.</p>
<div class="fragment"><div class="line">sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev</div><div class="line">sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev</div></div><!-- fragment --><p>to support gtk2: </p><div class="fragment"><div class="line">sudo apt-get install libgtk2.0-dev</div></div><!-- fragment --><p>to support gtk3: </p><div class="fragment"><div class="line">sudo apt-get install libgtk-3-dev</div></div><!-- fragment --><h3>Optional Dependencies</h3>
<p>Above dependencies are sufficient to install OpenCV in your Ubuntu machine. But depending upon your requirements, you may need some extra dependencies. A list of such optional dependencies are given below. You can either leave it or install it, your call :)</p>
<p>OpenCV comes with supporting files for image formats like PNG, JPEG, JPEG2000, TIFF, WebP etc. But it may be a little old. If you want to get latest libraries, you can install development files for system libraries of these formats.</p>
<div class="fragment"><div class="line">sudo apt-get install libpng-dev</div><div class="line">sudo apt-get install libjpeg-dev</div><div class="line">sudo apt-get install libopenexr-dev</div><div class="line">sudo apt-get install libtiff-dev</div><div class="line">sudo apt-get install libwebp-dev</div></div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>If you are using Ubuntu 16.04 you can also install <code>libjasper-dev</code> to add a system level support for the JPEG2000 format.</dd></dl>
<h3>Downloading OpenCV</h3>
<p>To download the latest source from OpenCV's <a href="https://github.com/opencv/opencv">GitHub Repository</a>. (If you want to contribute to OpenCV choose this. For that, you need to install <b>Git</b> first)</p>
<div class="fragment"><div class="line">$ sudo apt-get install git</div><div class="line">$ git clone https://github.com/opencv/opencv.git</div></div><!-- fragment --><p>It will create a folder "opencv" in current directory. The cloning may take some time depending upon your internet connection.</p>
<p>Now open a terminal window and navigate to the downloaded "opencv" folder. Create a new "build" folder and navigate to it.</p>
<div class="fragment"><div class="line">$ mkdir build</div><div class="line">$ cd build</div></div><!-- fragment --><h3>Configuring and Installing</h3>
<p>Now we have all the required dependencies, let's install OpenCV. Installation has to be configured with CMake. It specifies which modules are to be installed, installation path, which additional libraries to be used, whether documentation and examples to be compiled etc. Most of this work are done automatically with well configured default parameters.</p>
<p>Below command is normally used for configuration of OpenCV library build (executed from build folder):</p>
<div class="fragment"><div class="line">$ cmake ../</div></div><!-- fragment --><p>OpenCV defaults assume "Release" build type and installation path is "/usr/local". For additional information about CMake options refer to OpenCV <a class="el" href="../../d7/d9f/tutorial_linux_install.html">C++ compilation guide</a>:</p>
<p>You should see these lines in your CMake output (they mean that Python is properly found):</p>
<div class="fragment"><div class="line">--   Python 2:</div><div class="line">--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.6)</div><div class="line">--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)</div><div class="line">--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)</div><div class="line">--     packages path:               lib/python2.7/dist-packages</div><div class="line">--</div><div class="line">--   Python 3:</div><div class="line">--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)</div><div class="line">--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)</div><div class="line">--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)</div><div class="line">--     packages path:               lib/python3.4/dist-packages</div></div><!-- fragment --><p>Now you build the files using "make" command and install it using "make install" command.</p>
<div class="fragment"><div class="line">$ make</div><div class="line"># sudo make install</div></div><!-- fragment --><p>Installation is over. All files are installed in "/usr/local/" folder. Open a terminal and try import "cv2".</p>
<div class="fragment"><div class="line">import cv2 as cv</div><div class="line">print(cv.__version__)</div></div><!-- fragment --> </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:37 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
